<? ?>
<script type="text/javascript">
	$(function() {
		
		 $(".inputQty").keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) || 
             // Allow: home, end, left, right, down, up
            (e.keyCode >= 35 && e.keyCode <= 40)) {
                 // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });
		
		$('.inputQty').blur(function(){
			var v = $(this).val();
			if(v==""){
				v = 0;
				$(this).val("0");
			}
			var p = $(this).attr("item-price");
			var number = parseFloat(v) * parseFloat(p);
			$(this).next('span').html(fixedNumber(number));
			allPrice = 0;
			$.each($(this).parents('.bodyinput').find('.resultPrice'),function(a,b){
				var numPrice = $(b).html();
				numPrice = parseFloat(numPrice);
				allPrice+=numPrice;
			});
			var promo = $(this).parents('.bodyinput').parent('table').find('.allPromoPrice');
			var maxPrice = promo.attr("max-price");
			promo.html(addCommas(allPrice));
			maxPrice = maxPrice.replace(",","");
			maxPrice =  parseFloat(maxPrice)
			
			console.log("allprice" +  allPrice);
			console.log("max price" +  maxPrice);
			promo.parents('tr').find('td').css("background","white");
			if(allPrice > maxPrice){
				promo.parents('tr').find('td').css("background","red");
			}
			
			
			
			
			//.
			
			
		})
		
		$('#mySubmit,#mySubmit2').click(function() {
			
			
			if (!checkQty()) {
				alert("กรอกของแถมเกินมูลค่า");
				return false;
			}
			
			
			
		})
		/*
		$('.bodyinput').each(function(index, obj) {
			var $this = $(this);
			var maxKAR = $this.attr("data-max-KAR");
			var maxPAK = $this.attr("data-max-PAK");
			var maxEA = $this.attr("data-max-EA");
			$this.find('input[name="addKAR[]"]:eq(0)').val(maxKAR);
			$this.find('input[name="addPAK[]"]:eq(0)').val(maxPAK);
			$this.find('input[name="addEA[]"]:eq(0)').val(maxEA);
			
		});*/
		//$('#mySubmit,#mySubmit2').click();
	})
	function callOnPrice(){
		
	}
	function checkQty() {
		var process = true;
		$.each($('.allPromoPrice'),function(a,b){
			var v = $(this).html();
			v = parseFloat(v);
			var maxPrice = $(this).attr("max-price")
			maxPrice = maxPrice.replace(",","");
			var max =  parseFloat(maxPrice);
			
			if(v > max){
				process = false;
				return false;
			}
		})
		return process;
		
	}
</script>
<form  id="myPost" action="<?php echo $current?>tosummaryorder" method="post">
	<button id="mySubmit2" type="submit">ยืนยันการสร้างเอกสาร</button>
<input type="hidden" name="code" value="yes"/><h2 style="font-size:16px;font-weight:bold;">(<?php echo $customer->KUNNR?>)<?php echo $customer->NAME?></h2>
<table style="width:600px" class="myTable norborder" >
	<tr>
		<td style="width:30%">ที่อยู่</td>
		<td style="width:70%"><?php echo $customer->ADDRESS?></td>
	</tr>
	<tr>
		<td style="width:30%">โทรศัพท์</td>
		<td style="width:70%"><?php echo $customer->TELEPHONE?></td>
	</tr>
	<tr>
		<td style="width:30%">โทรสาร</td>
		<td style="width:70%"><?php echo $customer->FAX?></td>
	</tr>
	<tr>
		<td style="width:30%">อีเมล์</td>
		<td style="width:70%"><?php echo $customer->EMAIL?></td>
	</tr>
	<tr>
		<td style="width:30%">ประเภทลูกค้า</td>
		<td style="width:70%"><?php echo $customer->CUSTYP_TXT?></td>
	</tr>
	<tr>
		<td style="width:30%">ประเภทการเปิดบิล</td>
		<td style="width:70%">
			<?php echo $order->VatType?>
		</td>
	</tr>
	
	<tr>
		<td style="width:30%">ประเภทราคา</td>
		<td style="width:70%"><?php // echo $priceType?></td>
	</tr>
	
</table>
<br />

<table cellpadding="6" cellspacing="1"  border="0" class="myTable table2">

<tr>
  <th style="width:30%">สินค้า</th>
  <th style="text-align:right;width:10%">ราคาต่อหน่วย</th>
  <th style="text-align:center;width:10%">หน่วย</th>
  <th style="text-align:center;width:10%">จำนวน</th>
  <th style="text-align:center;width:10%">ราคารวม</th>
  
</tr>

<?php $i = 1; ?>
<?
$allDicount = 0;
?>
<?php foreach ($this->cart->contents() as $items): ?>

	<?
	$option = $this -> cart -> product_options($items['rowid']);
	$option = (array)$option;
	$pID = $option['MATNR'];
	$pName = $option['MAKTX'];
	$baseUnit = $option['BASE_UNIT'];
	$cartonRatio = $option['EA_CARTON'];
	$packRatio = $option['EA_PACK'];
	$itemPrice = $items['price'];
	if ($baseUnit == "KAR") {
		$b = "ลัง";
	} else if ($baseUnit == "PAK") {
		$b = "แพ็ก";
	} else {
		$b = "ชิ้น";
	}
	?>
	<?
	$style = "";
	?>
	<tr>
	  <td style="width:80%<?php echo $style?>"><?php echo $pID ?><br />
		<?php echo $pName ?></td>
		<td style="text-align:right;width:80px<?php echo $style?>">
	  	<div style="width:80px;float:right;float:right;"><?php echo $this -> cart -> format_number($items['price']); ?></div>
	  </td>
		<td style="width:10%;text-align: center<?php echo $style?>"><?php echo $b?></td>
	 <td style="width:20%;text-align: right;">
			<?php echo converUnit($items, $option); ?></td>
	  
	  <td style="text-align:right;width:80px<?php echo $style?>">
	  	<div style="width:90px;float:right;"><?php echo $this -> cart -> format_number($items['subtotal']); ?></div>
	  </td>
	  
	  
	</tr>
	

<?php $i++; ?>

<?php endforeach; ?>


<tfoot>
<tr>
  <td colspan="4"><strong>ราคารวม</strong></td>
  <td class="right" style="text-align: right"><?php echo $this -> cart -> format_number($this -> cart -> total()); ?></td>
  
</tr>
</tfoot>
</table>

<h2>รายการโปรโมชั่น</h2>
<?
	$p= 0;
	foreach($sumOfPromotion as $in=>$val){
		
	
		$desc = $val['PROMO_DESC'];
		
		
		
		
	?>
	<p><strong>รายละเอียดโปรโมชั่น:</strong>(<?php echo $desc->PROMO_ID?>) <?php echo $desc->PROMO_DESC?>
		
	<?
	foreach($val['promotionList'] as $j=>$k){
			
			
			
		$allDicount+= $k['DISCOUNT'];
		$k['NUM_PACK_QTY'] = $k['qty'] / $k['BASE_RATIO'];
		$k['ALL_PRICE_PREMIUM'] = ($k['NUM_PACK_QTY'])* round($itemPrice,2);
		
		
		
		
		
		/*
		 * <br /><strong>ของแถมที่ได้รับคือ:</strong> <?php echo $k['MAX_CARTON_QTY']?> ลัง 
		<?php echo $k['MAX_PAC_QTY']?> แพ็ก
		<?php echo $k['MAX_EA_QTY']?> ชิ้น 
		 * */
		
	?>
		
		<br /><strong>มูลค่าส่วนลด: </strong><?php echo number_format($k['DISCOUNT'],2)?> บาท
		<br /><strong>ของแถมรวมได้ไม่เกิน: </strong><?php echo number_format($k['ALL_PRICE_PREMIUM'],2)?> บาท
	
		</p>
<table cellpadding="6" cellspacing="1"  border="0" class="myTable table2" >
<tbody class="rootbody">
	
	
		<tr>
				<th style="width:55%">รายการแถม</th>
				<th style="width:10%">ชิ้น</th>
			</tr>
		<tbody class="bodyinput" max-car="<?php echo $k['MAX_CARTON_QTY']?>" max-pac="<?php echo $k['MAX_PAC_QTY']?>" max-ea="<?php echo $k['MAX_EA_QTY']?>">
			
				<?
				foreach($promotionList as $l=>$m){
					$m->$PRICE_COLUMN;
					$columnUnit = str_replace("_PRICE","_UNIT",$PRICE_COLUMN );
					$saleUnit = $m->$columnUnit;
					
					if($saleUnit != "" ){
					
					$s = explode("/", $saleUnit);
					
					$pricePerUnit = $s[1];
					if($pricePerUnit=="CAR"){
						$ratio =$m->EA_CARTON;
					}else if ($pricePerUnit=="PAC"){
						$ratio =$m->EA_PACK;
					}
					  
					
					$priceUnit = $m->$PRICE_COLUMN / $ratio;
					
				?>
				
				<tr>
			<td >
					<?php echo $m->MAKTX?><br />
					(<?php echo $m->MATNR?>)<br />
					<?
					
					?>
					ราคาต่อหน่วย <?php echo  number_format($priceUnit,2)?>
						
					
					</td>
			
			<td align="right">
				
					<input align="right" item-price="<?php echo number_format($priceUnit,2)?>" type="text" class="inputQty" style="width:100%" name="addEA[]" value="0" />
					 <span class="resultPrice">0.00</span>
				    <input type="hidden" name="premiumID[]" value="<?php echo $m->MATNR?>" />
				    <input type="hidden" name="itemPrice[]" value="<?php echo $priceUnit?>" />
			</td>
			<?
					}
			?>
		</tr>
		
				<?
					$p++;
				}
				?>
			</tbody>
			<tr>
				<td>รวมมูลค่าของแถมต้องไม่เกิน <?php echo number_format($k['ALL_PRICE_PREMIUM'],2)?></td>
				<td align="right" max-price="<?php echo number_format($k['ALL_PRICE_PREMIUM'],2)?>" class="allPromoPrice">0.00</td>
			</tr>
			</tbody>
</table>	
		<?
			}
		?>	
	
	<?
		
	
	}
	?>
<?
//}
?>
<br />
<a class="backButton" href="<?php echo base_url()?>order/editorder/<?php echo $this->session->userdata("CORDERID");?>">กลับไปยังสรุปรายการสั่งซื้อ</a> | 
<button id="mySubmit" type="submit" class="nextButton">สรุปรายการสั่งซื้อ</button>
<input type="hidden" name="discount" value="<?php echo $allDicount?>" />
<input type="hidden" name="priceType" value="<?php echo $PRICE_COLUMN?>" />
<input type="hidden" name="priceTypeUnit" value="<?php echo $PRICE_COLUMN_UNIT?>" />
<input type="hidden" name="orderid" value="<?php echo $this->session->userdata("CORDERID")?>" />
</form>